package com.google.project.client.HumanResources;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.project.client.Service.LoginService;
import com.google.project.client.Service.LoginServiceAsync;
import com.google.project.client.Service.MessageService;
import com.google.project.client.Service.MessageServiceAsync;
import com.google.project.client.Service.ProjectService;
import com.google.project.client.Service.ProjectServiceAsync;
import com.google.project.client.Service.UsersService;
import com.google.project.client.Service.UsersServiceAsync;
import com.google.project.shared.MessageDTO;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.UsersDTO;

public class HMHomepage extends Composite {
	
	@UiField Hyperlink hypCreateAcc;
	@UiField Hyperlink hypMessNew;
	@UiField Label lblTitle;
	@UiField Label lblMess;
	@UiField Label lblMess2;
	@UiField Hyperlink humanReqMessage;
	private static final Binder binder = GWT.create(Binder.class);
	private final LoginServiceAsync loginService = GWT.create(LoginService.class);
	private final MessageServiceAsync messageService = GWT.create(MessageService.class);
	private final UsersServiceAsync userService = GWT.create(UsersService.class);
	private final ProjectServiceAsync projectService = GWT.create(ProjectService.class);
	
	List<MessageDTO> listMessage = new ArrayList<MessageDTO>();
	MessageDTO messDTO = new MessageDTO();
	UsersDTO userDTO = new UsersDTO();
	
	interface Binder extends UiBinder<Widget, HMHomepage> {
	}

	public HMHomepage() {
		initWidget(binder.createAndBindUi(this));
		getAllAccNotCreate();
		getAllMess();
		getHumanReqMessage();
	}
	
	// get info user login from session
	public void getAllAccNotCreate() {
		loginService.getCurrentUserDTO(new AsyncCallback<UsersDTO>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(UsersDTO result) {
				userDTO = result;
				getAcc();
			}
		});
	}

	//Lay toan bo nhung nhan vien chua duoc tao tai khoan: User co status = 1
	public void getAcc() {
		userService.getUserListByStatus("1", new AsyncCallback<List<UsersDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<UsersDTO> result) {
				if(result.size() == 0){
					hypCreateAcc.setText("Danh sách nhân viên cần lập tài khoản mới: 0 (nhân viên)");
				}else{
					hypCreateAcc.setText("Danh sách nhân viên cần lập tài khoản mới: " +result.size()+ " (nhân viên)");
				}
				lblTitle.setText("THÔNG TIN TỔNG HỢP");
				lblMess.setText("Các vấn đề cần giải quyết :");
				lblMess2.setText("Tin nhắn mới:");
			}
		});
	}
	
	// get all Message
	public void getAllMess() {
		messageService.getMessageList(new AsyncCallback<List<MessageDTO>>() {

			@Override
			public void onSuccess(List<MessageDTO> result) {
				for (MessageDTO messageDTO : result) {
					listMessage.add(messageDTO);
				}
				loadMess();
			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});
	}
	
	//Lay thong tin Yeu cau nhan su cua cac du an moi - co isReq = 2 -chua dap ung hoac isReq=3 - dap ung chua du
	public void getHumanReqMessage() {
		projectService.getProjectList(new AsyncCallback<List<ProjectDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<ProjectDTO> result) {
				List<ProjectDTO> list = new ArrayList<ProjectDTO>();
				int size = 0;
				for (ProjectDTO projectDTO : result) {
					if(projectDTO.getStatus()!=2 &&projectDTO.getStatus()!=3  && (projectDTO.getIsReq()==2 || projectDTO.getIsReq()==3)){
						list.add(projectDTO);
						size ++;
					}
				}
				humanReqMessage.setText("Yêu cầu nhân sự: " + size + " (Yêu cầu)");
			}
		});
	}
	
	// get Message where (userId=receiveId and status=1) or (userId=senderId and
	// status=2) & parentID=0
	private void loadMess() {
		Boolean condition = true;
		List<MessageDTO> temple = new ArrayList<MessageDTO>();

		for (MessageDTO mess : listMessage) {
			condition = condition && (mess.getTypeId() == 1)
					&& (mess.getParentId().equalsIgnoreCase("0") && (((mess.getStatus() == 1)
							&& mess.getReceiverId().equals(userDTO.getUserId()) || ((mess.getStatus() == 2) && mess.getSenderId().equals(userDTO.getUserId())))));
			if (condition) {
				temple.add(mess);
			}
			condition = true;
		}
		//đếm tin nhắn mới
		if (temple != null) {
			hypMessNew.setText("Tin nhắn chưa đọc: " + temple.size() + " (tin nhắn)");
		}else{
			hypMessNew.setText("Tin nhắn chưa đọc: 0 (tin nhắn)");
		}
	}
	
	//Account can lap
	@UiHandler("hypCreateAcc")
	void onHypCreateAccClick(ClickEvent event) {
		int status = 1;	//Nhung nhan vien chua co tai khoan
		HMEntrypoint.get().HMStaffList(status);
	}
	
	//tin nhắn mới
	@UiHandler("hypMessNew")
	void onHypMessNewClick(ClickEvent event) {
		HMEntrypoint.get().HMMessList();
	}
	@UiHandler("humanReqMessage")
	void onHumanReqMessageClick(ClickEvent event) {
		HMEntrypoint.get().HMHumanReq();	//Lay ra nhung yeu cau nhan su da duoc phe duyet
	}
}
